﻿/*  Copyright (c) 2012 William Rogers and James Boud

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

using System;
using SlimDX;
using SlimDX.Direct3D11;

namespace JBBRXG11
{
    /// <summary>
    /// Contains rasterizer state, which determines how to convert vector data
    /// (shapes) into raster data (pixels).
    /// </summary>
    public class RasterizerState : IDisposable
    {
        /// <summary>
        /// A built-in state object with settings for not culling any primitives.
        /// </summary>
        public static RasterizerState CullNone;

        /// <summary>
        /// A built-in state object with settings for culling primitives with
        /// clockwise winding order.
        /// </summary>
        public static RasterizerState CullClockwise;

        /// <summary>
        /// A built-in state object with settings for culling primitives with
        /// counter-clockwise winding order.
        /// </summary>
        public static RasterizerState CullCounterClockwise;

        static RasterizerState()
        {
            CullNone = new RasterizerState();
            CullNone.CullMode = Microsoft.Xna.Framework.Graphics.CullMode.None;

            CullClockwise = new RasterizerState();
            CullClockwise.CullMode = Microsoft.Xna.Framework.Graphics.CullMode.CullClockwiseFace;

            CullCounterClockwise = new RasterizerState();
            CullCounterClockwise.CullMode = Microsoft.Xna.Framework.Graphics.CullMode.CullCounterClockwiseFace;
        }

        RasterizerStateDescription rsdesc;
        SlimDX.Direct3D11.RasterizerState dxrasterizerstate;

        internal SlimDX.Direct3D11.RasterizerState Get_DX_RasterizerState(Device dx_dev)
        {
            if (dxrasterizerstate == null)
                dxrasterizerstate = SlimDX.Direct3D11.RasterizerState.FromDescription(dx_dev, rsdesc);
            return dxrasterizerstate;
        }

        private void modified()
        {
            if (dxrasterizerstate != null)
            {
                dxrasterizerstate.Dispose();
                dxrasterizerstate = null;
            }
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////// Constructors ////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public RasterizerState()
        {
            rsdesc = new RasterizerStateDescription();
            rsdesc.CullMode = SlimDX.Direct3D11.CullMode.Front;
            rsdesc.DepthBias = 0;
            rsdesc.DepthBiasClamp = 0;
            rsdesc.FillMode = SlimDX.Direct3D11.FillMode.Solid;
            rsdesc.IsAntialiasedLineEnabled = false;  // TODO:  ????
            rsdesc.IsDepthClipEnabled = true;  // TODO:  ????
            rsdesc.IsFrontCounterclockwise = true;
            rsdesc.IsMultisampleEnabled = true;  // TODO:  ????
            rsdesc.IsScissorEnabled = false;
            rsdesc.SlopeScaledDepthBias = 0;
            dxrasterizerstate = null;

            GraphicsDeviceManager.GetGame(Game.GraphicsDevice).UserContentAdd(this);
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////// Properties //////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Gets or sets a bias value that takes into account the slope of a polygon.
        /// This bias value is applied to coplanar primitives to reduce aliasing and
        /// other rendering artifacts caused by z-fighting. The default is 0.
        /// </summary>
        public float SlopeScaleDepthBias { get { return rsdesc.SlopeScaledDepthBias; } set { rsdesc.SlopeScaledDepthBias = value;  modified(); } }

        /// <summary>
        /// Sets or retrieves the depth bias for polygons, which is the amount of
        /// bias to apply to the depth of a primitive to alleviate depth testing
        /// problems for primitives of similar depth. The default value is 0.
        /// </summary>
        public float DepthBias { get { return rsdesc.DepthBias; } set { rsdesc.DepthBias = (int)value; modified(); } }

        /// <summary>
        /// Enables or disables multisample antialiasing. The default is true.
        /// </summary>
        public bool MultiSampleAntiAlias { get { return rsdesc.IsMultisampleEnabled; } set { rsdesc.IsMultisampleEnabled = value; modified(); } }

        /// <summary>
        /// Enables or disables scissor testing. The default is false.
        /// </summary>
        public bool ScissorTestEnable { get { return rsdesc.IsScissorEnabled; } set { rsdesc.IsScissorEnabled = value; modified(); } }

        /// <summary>
        /// The fill mode, which defines how a triangle is filled during rendering.
        /// The default is FillMode.Solid.
        /// </summary>
        public Microsoft.Xna.Framework.Graphics.FillMode FillMode
        {
            get { return EnumConversions.convert(rsdesc.FillMode); }
            set { rsdesc.FillMode = EnumConversions.convert(value); modified(); }
        }

        /// <summary>
        /// Specifies the conditions for culling or removing triangles. The default
        /// value is CullMode.CounterClockwise.
        /// </summary>
        public Microsoft.Xna.Framework.Graphics.CullMode CullMode
        {
            get { return EnumConversions.convert(rsdesc.CullMode); }
            set { rsdesc.CullMode = EnumConversions.convert(value); modified(); }
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////// Methods /////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Immediately releases the unmanaged resources used by this object.
        /// </summary>
        public void Dispose()
        {
            if (dxrasterizerstate != null)
            {
                dxrasterizerstate.Dispose();
            }
        }
    }
}
